
/* GCSx
** TILESETPROP.CPP
**
** Tileset properties dialog
*/

/*****************************************************************************
** Copyright (C) 2003-2006 Janson
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
*****************************************************************************/

#include "all.h"

// Properties dialog

TileSetPropertiesDialog* TileSetPropertiesDialog::dialogFontSet = NULL;
TileSetPropertiesDialog* TileSetPropertiesDialog::dialogTileSet = NULL;

TileSetPropertiesDialog* TileSetPropertiesDialog::createFontSet() { start_func
    if (!dialogFontSet) {
        dialogFontSet = new TileSetPropertiesDialog(1);
    }
    return dialogFontSet;
}

TileSetPropertiesDialog* TileSetPropertiesDialog::createTileSet() { start_func
    if (!dialogTileSet) {
        dialogTileSet = new TileSetPropertiesDialog(0);
    }
    return dialogTileSet;
}

void TileSetPropertiesDialog::destroy() { start_func
    if (dialogFontSet) {
        delete dialogFontSet;
        dialogFontSet = NULL;
    }
    if (dialogTileSet) {
        delete dialogTileSet;
        dialogTileSet = NULL;
    }
}

TileSetPropertiesDialog::TileSetPropertiesDialog(int font) : Dialog(font ? "Font Properties" : "Image/Tile Set Properties") { start_func
    initialized = 0;
    isFont = font;
}

Dialog::ButtonAction TileSetPropertiesDialog::verifyEntry(int buttonId, Dialog::ButtonAction buttonType) { start_func
    if ((buttonType != BUTTON_APPLY) && (buttonType != BUTTON_OK)) return BUTTON_DEFAULT;

    // Another tileset with same name?
    string lName = dynamic_cast<WTextBox*>(findWidget(ID_NAME))->state();
    toLower(lName);
    const TileSet* found = tileset->getWorld()->findTileSet(lName);
    if ((found) && (found != tileset)) {
        guiErrorBox("A library by that name already exists- please choose another.", errorTitleDuplicateName);
        return BUTTON_NOTHING;
    }
    
    if (lName.empty()) {
        guiErrorBox("Please enter a name for this library.", errorTitleMissingName);
        return BUTTON_NOTHING;
    }

    // Count OK?
    int w = dynamic_cast<WNumberBox*>(findWidget(ID_WIDTH))->state();
    int h = dynamic_cast<WNumberBox*>(findWidget(ID_HEIGHT))->state();
    int count;
    if (isFont) count = dynamic_cast<WCheckBox*>(findWidget(ID_COUNT))->state() ? TileSet::TILE_FONTSET_EXT_COUNT : TileSet::TILE_FONTSET_NORMAL_COUNT;
    else count = dynamic_cast<WNumberBox*>(findWidget(ID_COUNT))->state();
    int sWidth, sHeight, perLine;
    int actualCount = TileSetEdit::calculateTileSurfaceSizing(w, h, count, sWidth, sHeight, perLine);
    assert(actualCount <= count);

    if (actualCount != count) {
        guiErrorBox(formatString("Too many images- the most %dx%d images you may have is %d.", w, h, actualCount), errorTitleImageOverflow);
        return BUTTON_NOTHING;
    }
    
    // @TODO: Reduction in tiles or size causes warning if not new
    // @TODO: Loss of tiles used within a layer causes more detailed warning
    
    return Dialog::verifyEntry(buttonId, buttonType);
}

int TileSetPropertiesDialog::run(string* title, int* width, int* height, int* count, int* maps, int* fixedWidth, int* defaultTransparent, const TileSet* whichTileset) { start_func
    tileset = whichTileset;

    int fixedPerLine = *fixedWidth ? 1 : 0;
    int numPerLine = *fixedWidth;
    if (numPerLine < 1) numPerLine = 16;
    int extendedFont = 0;
    if (*count == TileSet::TILE_FONTSET_EXT_COUNT) extendedFont = 1;
    
    if (!initialized) {
        Widget* w = NULL;
        WCheckBox* c = NULL;
    
        w = new WStatic(ID_LABEL, "\tName:");
        w->addTo(this);

        w = new WTextBox(ID_NAME, title, 0);
        w->addTo(this);

        w = new WStatic(ID_LABEL, "\tWidth:");
        w->addTo(this);

        w = new WNumberBox(ID_WIDTH, width, MIN_TILESIZE, MAX_TILESIZE);
        w->addTo(this);

        w = new WStatic(ID_LABEL, "\tHeight:");
        w->addTo(this);
        
        w = new WNumberBox(ID_HEIGHT, height, MIN_TILESIZE, MAX_TILESIZE);
        w->addTo(this);

        if (isFont) {
            w = new WCheckBox(ID_COUNT, "\tExtended font (224 characters)", &extendedFont, 1);
            w->addTo(this);
        }
        else {
            w = new WStatic(ID_LABEL, "N\tumber of images:");
            w->addTo(this);

            w = new WNumberBox(ID_COUNT, count, 0, MAX_TILES);
            w->addTo(this);
            
            w = new WStatic(ID_LABEL, "Number of \tcollision maps:");
            w->addTo(this);

            w = new WNumberBox(ID_MAPS, maps, 0, MAX_COLLMAPS);
            w->setToolTip("Set to zero if you do not need pixel-based collision maps");
            w->addTo(this);

            w = new WRadioButton(ID_TRANSPARENT, "Default to \ttransparent", defaultTransparent, 1);
            w->setToolTip("Blank tiles will be fully transparent");
            w->addTo(this);

            w = new WRadioButton(ID_TRANSPARENT, "Default to \tsolid black", defaultTransparent, 0);
            w->setToolTip("Blank tiles will be solid, opaque black");
            w->addTo(this);

            c = new WCheckBox(ID_FIXED, "Fi\txed image count per line", &fixedPerLine, 1);
            c->affectControl(ID_FIXEDCOUNT, 1);
            c->setToolTip("If not set, images will rearrange to fill the browsing window");
            c->addTo(this);

            w = new WStatic(ID_LABEL, "\tImages per line:");
            w->addTo(this);
            
            w = new WNumberBox(ID_FIXEDCOUNT, &numPerLine, 1, MAX_TILES);
            w->addTo(this);
        }

        w = new WButton(ID_OK, messageBoxOK, BUTTON_OK);
        w->addTo(this);
        
        w = new WButton(ID_CANCEL, messageBoxCancel, BUTTON_CANCEL);
        w->addTo(this);
        
        makePretty();
        
        initialized = 1;
    }
    else {
        findWidget(ID_NAME)->changeStorage(title);
        findWidget(ID_WIDTH)->changeStorage(width);
        findWidget(ID_HEIGHT)->changeStorage(height);
        if (isFont) {
            findWidget(ID_COUNT)->changeStorage(&extendedFont);
        }
        else {
            findWidget(ID_COUNT)->changeStorage(count);
            findWidget(ID_MAPS)->changeStorage(maps);
            findWidget(ID_FIXED)->changeStorage(&fixedPerLine);
            findWidget(ID_FIXEDCOUNT)->changeStorage(&numPerLine);
            findWidget(ID_TRANSPARENT, 0)->changeStorage(defaultTransparent);
            findWidget(ID_TRANSPARENT, 1)->changeStorage(defaultTransparent);
        }
    }
    
    // Hit OK?
    if (runModal() == ID_OK) {
        *fixedWidth = fixedPerLine ? numPerLine : 0;
        if (isFont) {
            *count = extendedFont ? TileSet::TILE_FONTSET_EXT_COUNT : TileSet::TILE_FONTSET_NORMAL_COUNT;
            *maps = 0;
        }
        return 1;
    }
    return 0;
}

